/**
  ******************************************************************************
  * @file           : studio_image_base.h
  * @author         : wangyingjie
  * @brief          : None
  * @attention      : None
  * @date           : 2025/6/4
  ******************************************************************************
  */

#ifndef STUDIO_IMAGE_BASE_H
#define STUDIO_IMAGE_BASE_H

#include "studio_color.h"

class studio_base
{
public:
    /// <summary>
    /// 创建指定宽高和颜色类型的空图像,颜色为全为0
    /// </summary>
    /// <param name="width"></param>
    /// <param name="height"></param>
    /// <param name="type"></param>
    /// <returns></returns>
    virtual bool create(const size_t& width, const size_t& height, const studio_color::type& type, const uint8_t& depth = 8) = 0;

    /// <summary>
    /// 从文件读取图像
    /// </summary>
    /// <param name="file"></param>
    /// <returns></returns>
    virtual bool read(const std::string& file) = 0;

    /// <summary>
    /// 将图像写入文件
    /// </summary>
    /// <param name="file"></param>
    /// <returns></returns>
    virtual bool write(const std::string& file) const = 0;

    /// <summary>
    /// 设置颜色
    /// </summary>
    /// <param name="row"></param>
    /// <param name="col"></param>
    /// <param name="pixel"></param>
    virtual void pixel(const size_t& row, const size_t& col, const studio_color& pixel) = 0;

    /// <summary>
    /// 获取指定位置的颜色
    /// </summary>
    /// <param name="row"></param>
    /// <param name="col"></param>
    /// <returns></returns>
    virtual studio_color pixel(const size_t& row, const size_t& col) const = 0;

    /// <summary>
    /// 从二进制数据创建图像
    /// </summary>
    /// <param name="bin"></param>
    /// <returns></returns>
    virtual bool decode(const std::string& bin) = 0;

    /// <summary>
    /// 将图像转换为二进制数据
    /// </summary>
    /// <returns></returns>
    virtual std::string encode() const = 0;

    /// <summary>
    /// 判断是否为有效的图像
    /// </summary>
    /// <param name="bin"></param>
    /// <returns></returns>
    bool valid(const std::string& bin);
    bool valid(const char* data, const size_t len);

    /// <summary>
    /// 判断是否为空
    /// </summary>
    /// <returns></returns>
    bool empty();

    /// <summary>
    /// 主动释放内存
    /// </summary>
    /// <returns></returns>
    void release();

    size_t width() const
    {
        return m_width;
    }

    size_t height() const
    {
        return m_height;
    }

    uint8_t pixel_size() const
    {
        return m_pixel_size;
    }

    uint8_t depth() const
    {
        return m_depth;
    }

    studio_color::type type() const
    {
        return m_type;
    }

    uint8_t* bytes()
    {
        return m_bytes.data();
    }

    std::string err() const
    {
        return m_err;
    }

    void compress_level(const int& level)
    {
        m_compress_level = level;
    }

protected:
    std::vector<uint8_t> m_bytes; // 二进制数据
    size_t m_width{0};            // 宽度
    size_t m_height{0};           // 高度
    uint8_t m_channels{0};        // 通道数
    uint8_t m_depth{0};           // 位深度
    int m_compress_level = 6;     // 压缩等级,[1,9], 1最快,压缩率最低, 9最慢,压缩率最高,以libpng为准,其他库根据这个自信调整
    uint8_t m_pixel_size{0};
    studio_color::type m_type{2}; // 颜色类型
    std::vector<uint8_t> HEADER;  // 固定头部,由于判断类型
    std::string m_err;
    //unsigned char** m_nbytes = nullptr;
};


#endif //STUDIO_IMAGE_BASE_H